<{extend name="public/base"}>
<{block name="foot_source"}>
<script type="text/javascript" src="/static/webuploader/webuploader.js"></script>
<link rel="stylesheet" type="text/css" href="/static/webuploader/webuploader.css">
<{/block}>
<{block name="content"}>
<!-- left column -->
<div class="col-md-12">
    <!-- general form elements -->
    <div class="box box-primary">
        <div class="box-header with-border">
            <h3 class="box-title"><{$title}></h3>
        </div>
        <!-- /.box-header -->
        <!-- form start -->
        <form role="form" action="<?php echo url('admin/purchase/update'); ?>" method="post">
            <div class="box-body">
                <div class="form-group">
                    <label for="title">活动标题</label>
                    <input type="text" class="form-control" id="title" name="title" value="<{$model.title}>" placeholder="请输入活动标题">
                </div>
                <div class="form-group">
                    <label for="type">活动类型</label>
                    <select name="type" id="type" class="form-control  select2 select2-hidden-accessible">
                        <option value="1" <{if condition="$model.type eq 1"}>selected <{/if}> >工厂</option>
                        <option value="0" <{if condition="$model.type eq 0"}>selected <{/if}>>自营</option>
                    </select>
                </div>
                <div class="form-group">
                    <label for="title">起止时间</label>
                    <input type="text" class="form-control" id="time_ranger" name="time_ranger"  value="<?php echo date('m/d/Y',$model['start_time']); ?> - <?php echo date('m/d/Y',$model['end_time']); ?>" placeholder="请选择活动起止时间">
                </div>


                <div class="form-group">
                    <table id="styles" class="table table-bordered table-striped">
                        <thead>
                        <tr>
                            <th>风格</th>
                            <th>品牌</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>

                        <!-- 风格和品牌列表 -->
                        <?php foreach($model['related'] as $key => $val ){ ?>
                        <tr>
                            <td><?php echo $styles[$key] ?></td> <td></td> <td><a href='javascript:;' class='btn btn-primary add_brand' data-oid='<{$key}>' data-title='添加品牌'>添加品牌</a> <a href='javascript:;' class='btn btn-danger remove_style' data-oid='<{$key}>' data-title='添加品牌'>移除风格</a></td>
                        </tr>
                            <?php if(!empty($val)){ ?>
                                <?php foreach($val as $kk => $vv ) {?>
                                    <tr class='style_<{$key}>'><td></td> <td><{$brands[$kk]}></td> <td><a href='javascript:;' class='btn btn-danger remove_brand ' data-oid='<{$kk}>' data-title='添加品牌'>移除</a></td></tr>
                                <?php } ?>
                            <?php } ?>
                        <?php } ?>

                        </tbody>
                        <tfoot>
                        <tr>
                            <th>风格</th>
                            <th>品牌</th>
                            <th>操作</th>
                        </tr>
                        </tfoot>
                    </table>
                </div>

                <div class="form-group">
                    <label></label>
                    <a class="btn btn-warning" id="add_style" data-title="添加风格">添加风格</a>
                </div>

                <input type="hidden" value="<{$model.id}>" name="id" />

            </div>
            <!-- /.box-body -->

            <div class="box-footer">
                <a href="javascript:;"   class="btn btn-primary" id="submit">保存</a>
                <a href="<?php echo url('admin/purchase/index'); ?>"   class="btn btn-default" id="cancel">返回</a>
            </div>
        </form>
    </div>
    <!-- /.box -->
</div>


<!-- 弹出框 -->
<div class="modal fade" id="modal">
    <div class="modal-dialog" style="width: auto;" >
        <div class="modal-content" style="width: 60%;margin:0 auto;">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title" id="modal_title">封面模板</h4>
            </div>
            <div class="modal-body">
                <iframe id="modalIframe" width="100%" height="500px" frameborder="0"></iframe>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default " data-dismiss="modal">    关  闭    </button>
            </div>
        </div>
    </div>
</div>


<{/block}>
<{block name="foot_script"}>

<!-- 配置文件 -->
<script type="text/javascript" src="/static/ueditor/ueditor.config.js"></script>
<!-- 编辑器源码文件 -->
<script type="text/javascript" src="/static/ueditor/ueditor.all.js"></script>
<!-- 实例化编辑器 -->
<script type="text/javascript">



    $(function(){
        var json_exists_styles = '<{$exists_styles}>';
        var json_exists_brand = '<{$exists_brand}>';
        //var exists_styles = {};//品牌后台排重，因为品牌不需要继续往下选择了
        //var exists_brand = {};
        var exists_styles = JSON.parse(json_exists_styles);
        var exists_brand = JSON.parse(json_exists_brand);
        console.log(exists_styles);
        console.log(exists_brand);

        var request_type = 1;
        var $cur_brand = null;
        var $cur_style_id = 0; //记录当前操作的风格
        window.modalCallback = function(oid){
            var api_url = '';
            if(request_type == 1){
                api_url = '<?php echo url("admin/data/getStyle"); ?>';
                $.get(api_url,{oid:oid},function(data,status){
                    if(data.code == 1){
                        data = data.data;
                        if(exists_styles[oid]){
                            alert("已经添加该风格了");
                            return;
                        }
                        exists_styles[oid] = oid;
                        exists_brand[oid] = {};
                        $("#styles").append("<tr><td>"+data.name+"</td> <td></td> <td><a href='javascript:;' class='btn btn-primary add_brand' data-oid='"+data.oid+"' data-title='添加品牌'>添加品牌</a> <a href='javascript:;' class='btn btn-danger remove_style' data-oid='"+data.oid+"' data-title='添加品牌'>移除风格</a></td></tr>");
                    }else{
                        alert(data.msg);
                    }
                });
            }else{
                api_url = '<?php echo url("admin/data/getBrand"); ?>';
                $.get(api_url,{oid:oid},function(data){
                    if(data.code == 1){
                        console.log($cur_style_id);
                        data = data.data;
                        if(exists_brand[$cur_style_id][oid]){
                            alert("已经添加该品牌了");
                            return;
                        }
                        exists_brand[$cur_style_id][oid] = oid;
                        $cur_brand.after("<tr class='style_"+data.soid+"'><td></td> <td>"+data.name+"</td> <td><a href='javascript:;' class='btn btn-danger remove_brand ' data-oid='"+data.oid+"' data-title='添加品牌'>移除</a></td></tr>");
                    }
                });
            }

        };

        $("#add_style").click(function(){
            request_type = 1;
            var style_url = "<?php echo url('admin/data/getStyles'); ?>";
            var title = $(this).data('title');
            $("#modal_title").html(title);
            $("#modalIframe").attr('src',style_url);
            $("#modal").modal();
        });

        $("#styles").delegate(".add_brand","click",function(){
            request_type = 2;
            var brand_url = "<?php echo url('admin/data/getBrands'); ?>";
            var oid = $(this).data('oid');
            brand_url = brand_url+"?oid="+oid;
            var title = $(this).data('title');
            $cur_brand = $(this).parent().parent();
            $cur_style_id = oid;
            $("#modal_title").html(title);
            $("#modalIframe").attr('src',brand_url);
            $("#modal").modal();
        });

        //移除风格和所有的 该风格的品牌
        $("#styles").delegate(".remove_style","click",function(){
            var oid = $(this).data('oid');
            $(".style_"+oid).each(function(index,item){
                $(this).remove();
            });
            $(this).parent().parent().remove();

        });

        $("#styles").delegate(".remove_brand","click",function(){
            $(this).parent().parent().remove();
        });



        $('#time_ranger').daterangepicker({
            autoclose: true
        });


        $("#submit").click(function(){
            var data = {};
            data['title'] = $("#title").val();
            data['type']  = $("#type").val();
            data['time_ranger'] = $("#time_ranger").val();
            data['style'] = exists_styles;
            data['brand'] = exists_brand;
            data['id'] = '<{$model.id}>';
            console.log(data);
            var submit_url = '<?php echo url("admin/purchase/update"); ?>';
            $.post(submit_url,data,function(data){
                alert(data.msg);
                if(data.code == 1){
                    window.location.reload();
                }
            });
        });
    });





</script>
<{/block}>

